# app/models/server.py
from app import db
from datetime import datetime

class Server(db.Model):
    __tablename__ = 'servers'

    server_id = db.Column(db.String(36), primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    owner_id = db.Column(db.String(36), db.ForeignKey('users.user_id'), nullable=False)
    invite_code = db.Column(db.String(12), unique=True, nullable=False)
    is_default = db.Column(db.Boolean, default=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)

    owner = db.relationship('User', back_populates='owned_servers', foreign_keys=[owner_id])
    channels = db.relationship('Channel', back_populates='server')
    members = db.relationship('ServerMember', back_populates='server')
    direct_chats = db.relationship('DirectChat', back_populates='server_source_rel')

    def __repr__(self):
        return f'<Server {self.name}>'


class DefaultChannelTemplate(db.Model):
    __tablename__ = 'default_channel_templates'

    template_id = db.Column(db.String(36), primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    type = db.Column(db.String(10), nullable=False)  # 'text' or 'voice'
    position = db.Column(db.Integer, default=0)

    def __repr__(self):
        return f'<DefaultChannelTemplate {self.name} ({self.type})>'